package com.wanxing.main;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhny.wxginger.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import feedback.FeedbackActivity;

public class SetorFragment extends Fragment {
	private Button bt_opinion;
	
	
	private View mView;
	private TextView tv_greeting;
//	private Button bt_reset;
	private Button tv_about;
	private Button tv_call;
	private Button tv_feedback;
	
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		mView = inflater.inflate(R.layout.setor,container, false);
		init();
		
		return mView;
	}
	private void init() {
		// TODO Auto-generated method stub
		tv_greeting=(TextView) mView.findViewById(R.id.tv_greeting);
//		bt_reset=(Button) mView.findViewById(R.id.bt_resetlogin);
		tv_about=(Button) mView.findViewById(R.id.bt_aboutus);
		tv_call=(Button) mView.findViewById(R.id.bt_callus);
		tv_feedback=(Button) mView.findViewById(R.id.bt_opinion);

		/*if(Login.preferences.getString("AUTO", "").equals("yes")){

			bt.setText("取消自动登录");
		}else if(Login.preferences.getString("AUTO", "").equals("no")){
			bt.setText("未选中自动登录");
		}*/
		SimpleDateFormat dateFormat=new SimpleDateFormat("HH",Locale.getDefault());
		int hour=Integer.parseInt(dateFormat.format(new Date()));
		System.out.println("hour"+hour);
		if(hour>=0&&hour<6){
			tv_greeting.setText("尊敬的用户凌晨好");
		}else if(hour>=6&&hour<12){
			tv_greeting.setText("尊敬的用户早上好");
		}else if(hour>=12&&hour<18){
			tv_greeting.setText("尊敬的用户下午好");
		}else if(hour>=18&&hour<24){
			tv_greeting.setText("尊敬的用户晚上好");
		}


//		bt_reset.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View arg0) {
//				// TODO Auto-generated method stub
//				startActivity(new Intent(getActivity(),LoginActivity.class));
//				getActivity().finish();
//			}
//		});
		tv_about.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				LinearLayout layout=(LinearLayout) getActivity().getLayoutInflater().inflate(R.layout.about_dialog, null);
				AlertDialog.Builder builder=new Builder(getActivity());
				builder.setTitle("智慧农业实验室简介")
				.setView(layout)
				.setNegativeButton("关闭", new android.content.DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int arg1) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				}).show();
			}
		});
		tv_call.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				RelativeLayout relativeLayout=(RelativeLayout) getActivity().getLayoutInflater().inflate(R.layout.call_dialog, null);
				final TextView tv_phone=(TextView) relativeLayout.findViewById(R.id.tv_phone);
				ImageView iv_call=(ImageView) relativeLayout.findViewById(R.id.img_call);
				String phone=null;
				iv_call.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						String name=tv_phone.getText().toString().trim();
						if(name.equals("饶灵康")){
							Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+"18553806185"));
							getActivity().startActivity(intent);
						}
					}
				});
				AlertDialog.Builder builder1=new Builder(getActivity());
				builder1.setTitle("拨打电话")
				.setView(relativeLayout)
				.setNegativeButton("关闭", new android.content.DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int arg1) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				}).show();
			}
		});
		tv_feedback.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent =  new Intent(getActivity(), FeedbackActivity.class);
				startActivity(intent);
			}
		});
	}
}
